//
//  ArticleViewController.m
//  A Cup
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 doll. All rights reserved.
//

#import "ArticleViewController.h"
#import "dataBaseHandle.h"
#import "UMSocial.h"
@interface ArticleViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property(nonatomic, retain)NSMutableArray *articleArr;
@property(nonatomic, retain)Read *sourceRead;
@property(nonatomic, retain)UIView *smallView;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, assign)UIWebView *webView;
@property(nonatomic, assign)BOOL isshow;
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, assign)NSInteger num;
@property(nonatomic, assign)BOOL isSave;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation ArticleViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.articleArr = [NSMutableArray array];
        self.isSave = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self postdata];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xia"] style:UIBarButtonItemStylePlain target:self action:@selector(clickright)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:59/255.0 green:54/255.0 blue:24/255.0 alpha:1];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(turnBack)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:59/255.0 green:54/255.0 blue:24/255.0 alpha:1];
    
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 100, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activity];
    [self.view bringSubviewToFront:self.activity];
    [self.activity startAnimating];
    [_activity release];
}

- (void)turnBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//解析post数据
- (void)postdata
{
    if ([dataBaseHandle shareDB].state == 1) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager  manager];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.conid,@"contentid",@"1",@"client",@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85" ,@"deviceid",@"",@"auth",@"3.0.1",@"version",nil];
        [manager POST:@"http://api2.pianke.me/article/info" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSMutableDictionary *dic = responseObject;
            NSMutableDictionary *dataDic = dic[@"data"];
            NSMutableDictionary *shareDic = dataDic[@"shareinfo"];
            NSMutableDictionary *userDic = dataDic[@"userinfo"];
            Read *read = [[Read alloc] init];
            [read setValuesForKeysWithDictionary:dataDic];
            [read setValuesForKeysWithDictionary:shareDic];
            [read setValuesForKeysWithDictionary:userDic];
            [self.articleArr addObject:read];
            self.sourceRead =read;
            [self setwebview];
            [self creatrightView];
            if (self.articleArr.count > 0) {
                [self.activity stopAnimating];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络，请检查网络后重新尝试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

//创建右键弹出的View
- (void)creatrightView
{
    self.smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
    self.smallView.backgroundColor = [UIColor colorWithRed:207/255.0 green:133/255.0 blue:128/255.0 alpha:1];
    [self.webView addSubview:self.smallView];
    
    //收藏按钮
    UIButton *CollectBu = [UIButton buttonWithType:UIButtonTypeSystem];
    [CollectBu setBackgroundImage:[UIImage imageNamed:@"iconfont-iconfontshoucang"] forState:UIControlStateNormal];
    CollectBu.frame = CGRectMake(self.view.frame.size.width/2 - 110, 10, 30, 30);
    [self.smallView addSubview:CollectBu];
    [CollectBu addTarget:self action:@selector(clickcollect:) forControlEvents:UIControlEventTouchUpInside];
    //收藏Label
    UILabel *likelabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 110, 50, 30, 10)];
    likelabel.textAlignment = NSTextAlignmentCenter;
    likelabel.text = @"收藏";
    likelabel.font = [UIFont systemFontOfSize:10];
    [self.smallView addSubview:likelabel];
    [likelabel release];
    //评论按钮
    UIButton *conBu = [UIButton buttonWithType:UIButtonTypeSystem];
    [conBu setBackgroundImage:[UIImage imageNamed:@"iconfont-pinglun"] forState:UIControlStateNormal];
    conBu.frame = CGRectMake(self.view.frame.size.width/2 - 15, 10, 30, 30);
    [self.smallView addSubview:conBu];
    [conBu addTarget:self action:@selector(clickcon:) forControlEvents:UIControlEventTouchUpInside];
    //评论Label
    UILabel *comlabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 15, 50, 30, 10)];
    comlabel.textAlignment = NSTextAlignmentCenter;
    comlabel.text = @"评论";
    comlabel.font = [UIFont systemFontOfSize:10];
    [self.smallView addSubview:comlabel];
    [comlabel release];
    //分享按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    button.frame = CGRectMake(self.view.frame.size.width/2 + 80, 10, 30, 30);
    [self.smallView addSubview:button];
    [button addTarget:self action:@selector(clickshare:) forControlEvents:UIControlEventTouchUpInside];
    //分享Label
    UILabel *sharelabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 80, 50, 30, 10)];
    sharelabel.textAlignment = NSTextAlignmentCenter;
    sharelabel.text = @"分享";
    sharelabel.font = [UIFont systemFontOfSize:10];
    [self.smallView addSubview:sharelabel];
    [sharelabel release];
    
    //默认状态设置隐藏
    self.smallView.hidden =YES;
}


//创建webview视图
- (void)setwebview
{
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.webView loadHTMLString:self.sourceRead.html baseURL:nil];
    self.webView.backgroundColor = [UIColor whiteColor];
    //设置代理
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    [self.webView release];
}

- (void)clickright
{
    if (self.isshow == YES) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xia"]];
        self.smallView.hidden =YES;
        self.isshow = NO;
    }else if (self.isshow == NO) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-shang"]];
        self.smallView.hidden = NO;
        self.isshow = YES;
    }
}

//点击收藏按钮
- (void)clickcollect:(UIButton *)sender
{
       [[dataBaseHandle shareDB] openDB];
       [[dataBaseHandle shareDB] creatReadTable];
       NSMutableArray *arr = [[dataBaseHandle shareDB]selectRead];
       if (arr.count > 0) {
           for (Read *readmo in arr) {
               if ([readmo.title isEqual:self.sharename]) {
                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                   self.num = 1;
                   [alert show];
                   [alert release];
                   break;
               }
           }
        if (self.num != 1) {
            //将数据加入数据库
       [[dataBaseHandle shareDB] insertReadWithTitle:self.sharename picUrl:self.picUrl id:self.conid];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
           }
       }
       if (arr.count == 0) {
        [[dataBaseHandle shareDB] insertReadWithTitle:self.sharename picUrl:self.picUrl id:self.conid];
           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
           [alert show];
           [alert release];
       }
}

//点击分享按钮
- (void)clickshare:(UIButton *)sender
{
    NSString *str = self.sourceRead.pic;
    UIImage *shareImage = [UIImage imageNamed:str];
    NSString *shareText = self.sharename;
    NSString *shareURL = self.sourceRead.url;
    NSString *share = [NSString stringWithFormat:@"%@ %@",shareText,shareURL];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"553afad967e58e7cba0031e4" shareText:share  shareImage:shareImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQQ,UMShareToWechatTimeline,UMShareToYXSession, nil] delegate:self];
}

//点击评论按钮
- (void)clickcon:(UIButton *)sender
{
    CommentViewController *comVC = [[CommentViewController alloc] init];
    comVC.idstr = self.conid;
    [self.navigationController pushViewController:comVC animated:YES];
    [comVC release];
}

- (void )dealloc
{
    _webView.delegate = nil;
    [_articleArr release];
    [_webView release];
    [_sourceRead release];
    [_smallView release];
    [_activity release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
